﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;

namespace BySoft.Web.Controls
{
    public class SCTreeView : TreeView
    {
        #region FIELD
        private const string ROOT_VALUE = "";
        private const string ROOT_TEXT = "全部";
        private const string VALUE_ID = "ID";
        private const string VALUE_NAME = "NAME";
        private const string VALUE_PARENTID = "PID";
        private const string VALUE_URLPATH = "URLPATH";
        private const string FIELD_ROOT_VALUE = "RootValue";
        private const string FIELD_ROOT_TEXT = "RootText";
        private const string FIELD_ID = "DATAID";
        private const string FIELD_NAME = "DATANAME";
        private const string FIELD_PARENTID = "DATAPARENTID";
        private const string FIELD_URLPATH = "DATAURLPATH";

        private const string VALUE_GRADE = "Grade";

        /// <summary>
        /// 根值
        /// </summary>
        public string RootValue
        {
            get
            {
                if (null == ViewState[FIELD_ROOT_VALUE])
                {
                    return ROOT_VALUE;
                }
                else
                {
                    return (string)ViewState[FIELD_ROOT_VALUE];
                }
            }
            set
            {
                ViewState[FIELD_ROOT_VALUE] = value;
            }
        }
        /// <summary>
        /// 根内容
        /// </summary>
        public string RootText
        {
            get
            {
                if (null == ViewState[FIELD_ROOT_TEXT])
                {
                    return ROOT_TEXT;
                }
                else
                {
                    return (string)ViewState[FIELD_ROOT_TEXT];
                }
            }
            set
            {
                ViewState[FIELD_ROOT_TEXT] = value;
            }
        }

        public string ValueID
        {
            get
            {
                if (null == ViewState[FIELD_ID])
                {
                    return VALUE_ID;
                }
                else
                {
                    return (string)ViewState[FIELD_ID];
                }
            }
            set
            {
                ViewState[FIELD_ID] = value;
            }
        }

        public string ValueName
        {
            get
            {
                if (null == ViewState[FIELD_NAME])
                {
                    return VALUE_NAME;
                }
                else
                {
                    return (string)ViewState[FIELD_NAME];
                }
            }
            set
            {
                ViewState[FIELD_NAME] = value;
            }
        }

        public string ValueParentID
        {
            get
            {
                if (null == ViewState[FIELD_PARENTID])
                {
                    return VALUE_PARENTID;
                }
                else
                {
                    return (string)ViewState[FIELD_PARENTID];
                }
            }
            set
            {
                ViewState[FIELD_PARENTID] = value;
            }
        }

        public string ValueUrlPath
        {
            get
            {
                if (null == ViewState[FIELD_URLPATH])
                {
                    return VALUE_URLPATH;
                }
                else
                {
                    return (string)ViewState[FIELD_URLPATH];
                }
            }
            set
            {
                ViewState[FIELD_URLPATH] = value;
            }
        }
        #endregion

        public SCTreeView()
        {

        }

        private class TreeViewData
        {
            public string UrlPath { get; set; }
            public int PId { get; set; }
            public string Name { get; set; }
            public int Id { get; set; }
        }

        public string Value
        {
            get
            {
                if (this.SelectedNode != null)
                {
                    return this.SelectedNode.Value;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public string Text
        {
            get
            {
                if (this.SelectedNode != null)
                {
                    return this.SelectedNode.Text;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public DataTable DtData
        {
            set
            {
                //this.SelectedNodeStyle.BackColor = Color.Orange;
                this.NodeStyle.CssClass = "treenode";
                this.SelectedNodeStyle.CssClass = "treesel";
                InitTV(value);
            }
        }

        #region 没有父类,只有层数和ID
        public void SetFirst(DataTable dt)
        {
            this.Nodes.Clear();
            TreeNode node = new TreeNode(); //初始化根目录
            node.Text = RootText;
            node.Value = RootValue;
            this.Nodes.Add(node); //添加根目录到treeview控件
            if (dt == null || dt.Rows.Count < 1)
            {
                return;
            }
            

            foreach (DataRow item in dt.Rows)
            {
                TreeNode ndtep = new TreeNode();
                ndtep.Text = GetString(item[ValueName]); ////节点名称
                ndtep.Value = GetString(item[ValueID]);
                if (GetString(item["IsEnd"]).Trim().Equals("0"))
                {
                    //ndtep.ImageUrl = "../App_Themes/Common_CSS/images/treeadd.gif";
                }
                //node.NavigateUrl = GetString(row[ValueUrlPath]); ////节点路径
                node.ChildNodes.Add(ndtep);
            }
            this.CollapseAll();
            node.Expand();
        }

        public void SetData(TreeNode nd,DataTable dt)
        {
            nd.ChildNodes.Clear();
            if (dt == null || dt.Rows.Count < 1)
            {
                return;
            }
            foreach (DataRow item in dt.Rows)
            {
                TreeNode ndtep = new TreeNode();
                ndtep.Text = GetString(item[ValueName]); ////节点名称
                ndtep.Value = GetString(item[ValueID]);
                if (GetString(item["IsEnd"]).Trim().Equals("0"))
                {
                    //ndtep.ImageUrl = "../App_Themes/Common_CSS/images/treeadd.gif";
                }
                //node.NavigateUrl = GetString(row[ValueUrlPath]); ////节点路径
                nd.ChildNodes.Add(ndtep);
            }
            nd.Expand();
        }

        private void InitTV(DataTable dt)
        {
            this.Nodes.Clear();
            if (dt == null || dt.Rows.Count < 1)
            {
                return;
            }
            TreeNode node = new TreeNode(); //初始化根目录
            node.Text = RootText;
            node.Value = RootValue;
            this.Nodes.Add(node); //添加根目录到treeview控件

            AddNode(node, RootValue, 1, dt); //0为根节点
            this.CollapseAll();
            node.Expand();
        }

        private void AddNode(TreeNode nd, string NodeId, int iGrade, DataTable dt)
        {
            DataRow[] drs;
            if (iGrade == 1)
            {
                drs = dt.Select(VALUE_GRADE + "=" + iGrade);
            }
            else
            {
                drs = dt.Select(VALUE_GRADE + "=" + iGrade + " AND " + ValueID + " like '" + NodeId + "%'");
            }

            foreach (DataRow row in drs)
            {
                TreeNode node = new TreeNode();
                node.Text = GetString(row[ValueName]); ////节点名称
                node.Value = GetString(row[ValueID]);
                //node.NavigateUrl = GetString(row[ValueUrlPath]); ////节点路径
                nd.ChildNodes.Add(node);
                AddNode(node, node.Value, iGrade + 1, dt); //递归添加节点
            }
        }

        public bool AddNode(string strID, string strName, string strParentID)
        {
            TreeNode tn = GetTreeNode(strParentID);
            if (tn != null)
            {
                TreeNode tnadd = new TreeNode();
                tnadd.Value = strID;
                tnadd.Text = strName;
                //tnadd.Select();
                //tn.ImageUrl = "../App_Themes/Common_CSS/images/treeadd.gif";
                tn.ChildNodes.Add(tnadd);
                tn.Expand();
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ModifyNode(string strID, string strName)
        {
            TreeNode tn = GetTreeNode(strID);
            if (tn != null)
            {
                tn.Text = strName;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DelNode(string strID)
        {
            TreeNode tn = GetTreeNode(strID);
            if (tn != null)
            {
                TreeNode tntep = tn.Parent;
                tntep.ChildNodes.Remove(tn);
                if (tntep.ChildNodes.Count < 1)
                {
                    tntep.ImageUrl = "";
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public TreeNode GetTreeNode(string strid)
        {
            foreach (TreeNode item in this.Nodes)
            {
                if (item.Value.Equals(strid))
                {
                    return item;
                }
                if (item.ChildNodes.Count > 0)
                {
                    TreeNode tntep = GetTreeNode_Child(item, strid);
                    if (tntep != null)
                    {
                        return tntep;
                    }
                }
            }
            return null;
        }

        private TreeNode GetTreeNode_Child(TreeNode tn, string strid)
        {
            foreach (TreeNode item in tn.ChildNodes)
            {
                if (item.Value.Equals(strid))
                {
                    return item;
                }
                if (item.ChildNodes.Count > 0)
                {
                    TreeNode tntep = GetTreeNode_Child(item, strid);
                    if (tntep != null)
                    {
                        return tntep;
                    }
                }
            }
            return null;
        }
        #endregion

        #region comm
        public string GetString(object obj)
        {
            if (obj == null || obj == DBNull.Value)
                return "";
            else
                return obj.ToString();
        }

        protected override void OnSelectedNodeChanged(EventArgs e)
        {
            //int id = 0;
            //this.SelectedNode.ChildNodes.Clear();
            //if (!string.IsNullOrEmpty(this.SelectedNode.Value))
            //{
            //    id = Convert.ToInt32(this.SelectedNode.Value);
            //}
            //AddNode(this.SelectedNode, id);
            //this.SelectedNode.Expand();
            base.OnSelectedNodeChanged(e);
        }
        #endregion

        #region 以下为有父类的情况
        //private void InitTV(DataTable dt)
        //{
        //    this.Nodes.Clear();
        //    if (dt == null || dt.Rows.Count < 1)
        //    {
        //        return;
        //    }
        //    TreeNode node = new TreeNode(); //初始化根目录
        //    node.Text = RootText;
        //    node.Value = RootValue;
        //    this.Nodes.Add(node); //添加根目录到treeview控件

        //    AddNode(node, RootValue, dt); //0为根节点
        //    this.CollapseAll();
        //    node.Expand();
        //}

        //private void AddNode(TreeNode nd, string NodeId, DataTable dt)
        //{
        //    DataRow[] drs = dt.Select(ValueParentID + "=" + NodeId);

        //    foreach (DataRow row in drs)
        //    {
        //        TreeNode node = new TreeNode();
        //        node.Text = GetString(row[ValueName]); ////节点名称
        //        node.Value = GetString(row[ValueID]);
        //        //node.NavigateUrl = GetString(row[ValueUrlPath]); ////节点路径
        //        nd.ChildNodes.Add(node);
        //        AddNode(node, node.Value,dt); //递归添加节点
        //    }
        //}

        //public bool AddNode(string strID,string strName,string strParentID)
        //{
        //    TreeNode tn = GetTreeNode(strParentID);
        //    if (tn != null)
        //    {
        //        TreeNode tnadd = new TreeNode();
        //        tnadd.Value = strID;
        //        tnadd.Text = strName;
        //        //tnadd.Select();
        //        tn.ChildNodes.Add(tnadd);
        //        tn.Expand();
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        //public bool ModifyNode(string strID, string strName)
        //{
        //    TreeNode tn = GetTreeNode(strID);
        //    if (tn != null)
        //    {
        //        tn.Text = strName;
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        //public bool DelNode(string strID)
        //{
        //    TreeNode tn = GetTreeNode(strID);
        //    if (tn != null)
        //    {
        //        tn.Parent.ChildNodes.Remove(tn);
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        //public TreeNode GetTreeNode(string strid)
        //{
        //    foreach (TreeNode item in this.Nodes)
        //    {
        //        if (item.Value.Equals(strid))
        //        {
        //            return item;
        //        }
        //        if (item.ChildNodes.Count > 0)
        //        {
        //            TreeNode tntep = GetTreeNode_Child(item, strid);
        //            if (tntep != null)
        //            {
        //                return tntep;
        //            }
        //        }
        //    }
        //    return null;
        //}

        //private TreeNode GetTreeNode_Child(TreeNode tn, string strid)
        //{
        //    foreach (TreeNode item in tn.ChildNodes)
        //    {
        //        if (item.Value.Equals(strid))
        //        {
        //            return item;
        //        }
        //        if (item.ChildNodes.Count > 0)
        //        {
        //            TreeNode tntep = GetTreeNode_Child(item, strid);
        //            if (tntep != null)
        //            {
        //                return tntep;
        //            }
        //        }
        //    }
        //    return null;
        //}
        #endregion
    }
}
